package com.leejxx.health.service.impl;

import com.leejxx.health.dao.MemberDao;
import com.leejxx.health.pojo.Member;
import com.leejxx.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author LeeJx
 * @ClassName MemberServiceImpl
 * @Description TODO
 * @date 2021/1/6 14:48
 * @Version 1.0
 */
@DubboService
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    @Transactional
    public Member addIfNotExist(Member member) {
        Member pojo = memberDao.findByPhoneNumber(member.getPhoneNumber());
        //如果存在则更新，不存在则添加
        if (null != pojo) {
            member.setId(pojo.getId());
            memberDao.updateById(member);
        } else {
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        return memberDao.findByPhoneNumber(member.getPhoneNumber());
    }

    @Override
    public Member findById(Integer memberId) {
        return memberDao.findById(memberId);
    }
}
